//
//  OpenCell.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/9/3.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "OpenCell.h"
#import "OpenPhotoContainerView.h"
#import "UIView+SDAutoLayout.h"
#import "CellModel.h"
const CGFloat contentLabelFontSize = 15;
CGFloat maxContentLabelHeight = 0; // 根据具体font而定
@implementation OpenCell{
    UIImageView *_iconView;
    UILabel *_nameLable;
    UILabel *_contentLabel;
    OpenPhotoContainerView *_picContainerView;
    UIButton *_moreButton;
    UILabel *_oriPriceLb;
    UILabel *_priceLb;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self setup];
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setup
{
    
    _iconView = [UIImageView new];
    
    _nameLable = [UILabel new];
    _nameLable.font = [UIFont systemFontOfSize:14];
    _nameLable.textColor = [UIColor colorWithRed:(51 / 255.0) green:(51 / 255.0) blue:(51 / 255.0) alpha:1];
    
    _contentLabel = [UILabel new];
    _contentLabel.font = [UIFont systemFontOfSize:contentLabelFontSize];
    _contentLabel.numberOfLines = 0;
    if (maxContentLabelHeight == 0) {
        maxContentLabelHeight = _contentLabel.font.lineHeight * 5;
    }
    
    _moreButton = [UIButton new];
    [_moreButton setTitle:@"展示全文" forState:UIControlStateNormal];
    [_moreButton setTitleColor:[UIColor colorWithRed:92/255.0 green:140/255.0 blue:193/255.0 alpha:1.0] forState:UIControlStateNormal];
    [_moreButton addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
    
    _picContainerView = [OpenPhotoContainerView new];
    
    _priceLb = [UILabel new];
    _priceLb.font = [UIFont systemFontOfSize:16];
    _priceLb.textColor = [UIColor blackColor];
    
    _oriPriceLb = [UILabel new];
    _oriPriceLb.font = [UIFont systemFontOfSize:13];
    _oriPriceLb.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
    
     NSArray *views = @[_iconView, _nameLable, _contentLabel, _moreButton, _picContainerView,_priceLb,_oriPriceLb];
    [self.contentView sd_addSubviews:views];
    
    
    
    
    UIView *contentView = self.contentView;
    CGFloat margin = 10;
    
    _iconView.sd_layout
    .leftSpaceToView(contentView, margin)
    .topSpaceToView(contentView, margin + 5)
    .widthIs(40)
    .heightIs(40);
    
    _nameLable.sd_layout
    .leftSpaceToView(_iconView, margin)
    .centerYEqualToView(_iconView)
    .heightIs(16);
    [_nameLable setSingleLineAutoResizeWithMaxWidth:200];
    
    _contentLabel.sd_layout
    .leftEqualToView(_nameLable)
    .topSpaceToView(_iconView, margin)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _moreButton.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_contentLabel, 0)
    .widthIs(60);
    
    _picContainerView.sd_layout
    .leftEqualToView(_contentLabel); // 已经在内部实现宽度和高度自适应所以不需要再设置宽度高度，top值是具体有无图片在setModel方法中设置
    
    _priceLb.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_picContainerView, margin)
    .heightIs(20);
    [_priceLb setSingleLineAutoResizeWithMaxWidth:200];
    
    _oriPriceLb.sd_layout
    .leftSpaceToView(_priceLb, margin)
    .centerYEqualToView(_priceLb)
    .heightIs(15);
    [_oriPriceLb setSingleLineAutoResizeWithMaxWidth:200];
}

- (void)setModel:(CellModel *)model
{
    _model = model;
    
    _iconView.image = [UIImage imageNamed:model.iconName];
    _nameLable.text = model.name;
    _contentLabel.text = model.msgContent;
    _picContainerView.picPathStringsArray = model.imageArray;
    if (model.shouldShowMoreButton) { // 如果文字高度超过60
        _moreButton.sd_layout.heightIs(20);
        _moreButton.hidden = NO;
        if (model.isOpening) { // 如果需要展开
            _contentLabel.sd_layout.maxHeightIs(MAXFLOAT);
            [_moreButton setTitle:@"收起" forState:UIControlStateNormal];
        } else {
            _contentLabel.sd_layout.maxHeightIs(maxContentLabelHeight);
            [_moreButton setTitle:@"展示全文" forState:UIControlStateNormal];
        }
    } else {
        _moreButton.sd_layout.heightIs(0);
        _moreButton.hidden = YES;
    }
    
    CGFloat picContainerTopMargin = 0;
    if (model.imageArray.count) {
        picContainerTopMargin = 10;
    }
    _picContainerView.sd_layout.topSpaceToView(_moreButton, picContainerTopMargin);
    
    [self setupAutoHeightWithBottomView:_priceLb bottomMargin:15];
    
    _priceLb.text = @"¥123";
    NSString *oriPriceStr = @"¥620";
    NSArray *arr = [NSArray arrayWithObject:oriPriceStr];
    _oriPriceLb.attributedText = [self changeToAttributeStringWithSubstringArray:arr DeletelineColor:[UIColor grayColor] oriStr:oriPriceStr];
}


#pragma mark - private actions

- (void)moreButtonClicked
{
    if (self.moreButtonClickedBlock) {
        self.moreButtonClickedBlock(self.indexPath);
    }
}

- (NSMutableAttributedString *)changeToAttributeStringWithSubstringArray:(NSArray *)substringArray
                                                         DeletelineColor:(UIColor *)deletelineColor oriStr:(NSString *)str{
    
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:str];
    
    for (NSString *substring in substringArray) {
        
        NSRange range = [str rangeOfString:substring];
        [attributeString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:range];// 删除线的类型
        [attributeString addAttribute:NSStrikethroughColorAttributeName value:deletelineColor range:range];
    }
    
    return attributeString;
}

@end
